package com.example.client.appVersion

import com.example.commons.models.AppVersion
import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.call.body
import io.ktor.client.call.body
import io.ktor.client.engine.cio.*
import io.ktor.client.plugins.*
import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.client.plugins.logging.*
import io.ktor.client.request.*
import io.ktor.client.request.forms.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.serialization.kotlinx.json.*
import kotlinx.serialization.json.Json
import java.io.File


suspend fun main(): Unit {
    val client = HttpClient(CIO) {
        install(Logging) {
            logger = Logger.DEFAULT
            level = LogLevel.HEADERS
        }
        install(HttpTimeout) {
            requestTimeoutMillis = 30000
            connectTimeoutMillis = 30000
        }
        install(ContentNegotiation) {
            json(Json {
                prettyPrint = true
                isLenient = true
            })
        }
    }
    val response: HttpResponse = client.get("http://0.0.0.0:8080/appVersion")
    client.post("") {
//        content
        formData {
            append("apk", File("app.apk").readBytes(), Headers.build {
//                contentType(ContentType.Application.Json)
//                append(HttpHeaders.ContentType, "image/png")
                append(HttpHeaders.ContentDisposition, "filename=app.apk")
            })
            append("apk-metadata", "", Headers.build {
                set(HttpHeaders.ContentType, "application/json")
            })
        }
        this.onUpload { bytesSentTotal, contentLength ->

        }
    }
    val str = response.body<AppVersion>()
    println(str)
}